package com.example.noelwhereismystuffs.views.DisplayLostItemsActivity;

import java.util.ArrayList;
import java.util.List;

import com.example.noelproject.R;
import com.example.noelwhereismystuffs.models.Database;
import com.example.noelwhereismystuffs.models.DisplayLostItemsModel;
import com.example.noelwhereismystuffs.models.Item;
import com.example.noelwhereismystuffs.models.LostItem;

import com.example.noelwhereismystuffs.models.User;
import com.example.noelwhereismystuffs.presenters.DisplayLostItemsPresenter;
import com.example.noelwhereismystuffs.support.MyLostItemAdater;

import android.os.Bundle;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;

public class DisplayLostItemsActivity extends ListActivity implements
		IDisplayLostItemsView {
	/** The list of lost items to display */
	private List<Item> lostItemList;
	private static User myUser = User.NULL_USER;
	private static Database myData;
	private DisplayLostItemsPresenter myPresenter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// retrieve user from the UserProfileActivity
		myUser = (User) getIntent().getSerializableExtra("User");
		myData = (Database) getIntent().getSerializableExtra("Database");
		lostItemList = (List<Item>) getIntent()
				.getSerializableExtra("itemList");
		/** create the activity presenter */
		myPresenter = new DisplayLostItemsPresenter(new DisplayLostItemsModel(
				myData), this, myUser);
		View title = getLayoutInflater()
				.inflate(R.layout.lost_view_title, null);

		ListView listView = getListView();
		listView.addHeaderView(title);
		List<Item> testList = lostItemList;
		List<String> itemName = new ArrayList<String>();
		// now make an instance of our adapter so that the data can be displayed
		// MyLostItemAdater adapt = new MyLostItemAdater(this, lostItemList);
		for (Item i : lostItemList) {
			itemName.add(i.getName());
		}
		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, itemName);
		setListAdapter(adapt);
	}


	@Override
	public void setLostItemsList(List<Item> items) {
		this.lostItemList = items;
	}

	private Spinner getCategoriesField() {
		return (Spinner) findViewById(R.id.categoriesSpinnerListView);
	}

	private Spinner getStatusField() {
		return (Spinner) findViewById(R.id.statusSpinnerListView);
	}

	private EditText getDateField() {
		return (EditText) findViewById(R.id.dateField);
	}

	public void onClickFilter(View source) {
		String status = getStatusField().getSelectedItem().toString();
		String categories = getCategoriesField().getSelectedItem().toString();
		String date = getDateField().getText().toString();

		if (!status.equals("None")) {
			myPresenter.filtStatus(status.trim());
		} else if (!categories.equals("None")) {
			myPresenter.filtCategories(categories);
		} else if (!date.equals("") && date != null) {
			myPresenter.filtDate(date);
		} else {

			findViewById(R.id.dateField).requestFocus();
		}
	}

	@Override
	public void advanceFilter(List<Item> itemList) {
		List<String> itemName = new ArrayList<String>();
		for (Item i : itemList) {
			itemName.add(i.getName());
		}
		
		ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, itemName);
		setListAdapter(adapt);
	}

}
